package com.example.system_backend.controller;

import com.example.system_backend.dto.LoginDTO;
import com.example.system_backend.dto.RegisterDTO;
import com.example.system_backend.service.UserService;
import com.example.system_backend.util.ResultVOUtil;
import com.example.system_backend.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserHandler {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResultVO login(@RequestBody LoginDTO loginDTO) {
        Integer login_result = userService.login(loginDTO.getEmail(), loginDTO.getPassword());
        if (login_result == -1) {
            return ResultVOUtil.error("用户不存在");
        } else if (login_result == -2) {
            return ResultVOUtil.error("密码错误");
        } else {
            return ResultVOUtil.success(login_result, "登录成功");
        }
    }

    @PostMapping("/register")
    public ResultVO register(@RequestBody RegisterDTO registerDTO) {
        Integer register_result = userService.register(
                registerDTO.getEmail(),
                registerDTO.getName(),
                registerDTO.getPassword()
        );
        if (register_result == -1) {
            return ResultVOUtil.error("用户已存在");
        } else {
            return ResultVOUtil.success(null, "注册成功");
        }
    }
}
